<template>
	<view class="content">
		<scroll-view :scroll-into-view="topItem" scroll-with-animation scroll-y="true" class="scroll-cont"
			@scroll="handleScroll">
			<view v-for="(item,index) in homeLis" :key="index" class="box" @click="toArticle(item.objectId)"
				v-if="item.topImage.url">
				<image :src="item.topImage.url" />
				<p>{{item.title}}</p>
				<text>{{item.body}}</text>
			</view>
		</scroll-view>
		<view class="back-top" @tap="toTop">
			<text>↑</text>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				homeLis: [],
				objId: 0,
				topItem: '',
				isShow: false,
				isExit:[],
			}
		},
		methods: {
			getData() {
				uni.request({
					url: `http://cloud.agopage.com/6b83fd3bc06260ec/topPage?skip=${this.objId}&version=1.3.0`,
					success: (res) => {

						this.homeLis = res.data.agopages;
					}
				})
			},

			toArticle(id) {
				uni.navigateTo({
					url: `/pages/article/article?id=${id}`
				})
			},
			toTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100,
				});
			}

		},
		mounted() {
			this.objId = Math.floor(Math.random() * 1684)
			this.getData()
		},
		onPullDownRefresh() {
			this.objId = Math.floor(Math.random() * 1684)
			this.isExit.push(this.objId)
			uni.request({
				url: `http://cloud.agopage.com/6b83fd3bc06260ec/topPage?skip=${this.objId}&version=1.3.0`,
				success: (res) => {
					// console.log(res.data.agopages);
					this.homeLis = res.data.agopages;
				}
			})
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},

		onReachBottom() {
			
			this.objId = Math.floor(Math.random() * 1684)

			if (this.isExit.indexOf(this.objId) == -1) {
				uni.request({
					url: `http://cloud.agopage.com/6b83fd3bc06260ec/topPage?skip=${this.objId}&version=1.3.0`,
					success: (res) => {

						this.homeLis = [...this.homeLis, ...res.data.agopages];

					}
				});
			}
		},
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 80vw;
		margin-left: 10vw;
		margin-top: 10px;
		margin-bottom: 25px;
		height: 280px;
		border-radius: 8px;
		box-shadow: 0px 10px 5px #ccc;
		overflow: hidden;

		image {
			width: 80vw;
			height: 150px;
		}

		p {
			margin-top: 5px;
			font-size: 18px;
			width: 63vw;
			margin-left: 4vw;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		text {
			margin-top: 10px;
			font-size: 13px;
			overflow: hidden;
			text-overflow: ellipsis;
			width: 70vw;
			margin-left: 5vw;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 3;
			word-break: break-all;
		}
	}

	.foot {
		margin-top: 20px;
		height: 50px;
		line-height: 25px;
		background-color: #eee;
		font-size: 13px;
		text-align: center;
	}

	.back-top {
		height: 100rpx;
		width: 100rpx;
		background-color: #fff;
		border-radius: 50%;
		box-shadow: 0 0 10rpx 4rpx rgba(0, 0, 0, .4);
		position: fixed;
		bottom: 40rpx;
		right: 20rpx;
		text-align: center;
		line-height: 100rpx;
	}
</style>
